package com.WangNan.mall.controller;

import com.WangNan.mall.entity.Manager;
import com.WangNan.mall.service.ManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author :W.
 * @Date:2025/4/15 14:37
 */
@Slf4j
@Controller
@RequestMapping("admin/manager")
public class ManagerController {
    @Autowired
    private ManagerService managerService;

    @GetMapping("add")
    public String add() {
        return "manager_add";
    }

    @PostMapping("add")
    public String add(Manager manager) {
        manager.setStatus("y");
        managerService.add(manager);

        return "redirect:/admin/manager/list";
    }

    @GetMapping("list")
    public String list(Model model) {
        List<Manager> managers = managerService.list();
        model.addAttribute("managers", managers);
        return "manager_list";
    }

    @GetMapping("update/{id}")
    public String update(@PathVariable int id, Model model) {
        Manager manager = managerService.getById(id);
        model.addAttribute("manager", manager);
        return "manager_update";
    }

    @PostMapping("update")
    public String update(Manager manager) {
        log.debug("manager:{}", manager);
        managerService.updateById(manager);
        return "redirect:/admin/manager/list";
    }

    @GetMapping("del/{id}")
    public String del(@PathVariable int id){
        managerService.removeById(id);
        return "redirect:/admin/manager/list";
    }

}